<?php
// ---------------------------------------------------
//  Image management
// ---------------------------------------------------

/**
 * set memory_limit when create thumbnail
 * @param file $filename
 *
 * @return boolean true/false
 * @author maittt
 */
function setMemoryForImage( $filename ){
  $memoryLimitMB = (integer) ini_get('memory_limit');
  $imageInfo = getimagesize($filename);
  $MB = 1048576;  // number of bytes in 1M
  $K64 = 65536;    // number of bytes in 64K
  $TWEAKFACTOR = 1.5;  // Or whatever works for you
  $memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
  * $imageInfo['bits']
  * $imageInfo['channels'] / 8
  + $K64
  ) * $TWEAKFACTOR
  );
  //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also
  //Default memory limit is 8MB so well stick with that.
  //To find out what yours is, view your php.ini file.
  $memoryLimit = 8 * $MB;
  if (function_exists('memory_get_usage') &&
  memory_get_usage() + $memoryNeeded > $memoryLimit)
  {
    $newLimit = $memoryLimitMB + ceil( ( memory_get_usage()
    + $memoryNeeded
    - $memoryLimit
    ) / $MB
    );
    ini_set( 'memory_limit', $newLimit . 'M' );
    return true;
  }else{
    return false;
  }
}
/**
 * Open image file
 *
 * This function will try to open image file
 *
 * @param string $file
 * @return resource
 */
function open_image($file) {
  if(!extension_loaded('gd')) {
    return false;
  } // if
  setMemoryForImage($file);
  $info =& getimagesize($file);
  if($info) {
    switch($info[2]) {
      case IMAGETYPE_JPEG:
        return array(
            'type' => IMAGETYPE_JPEG,
            'resource' => imagecreatefromjpeg($file)
        ); // array
      case IMAGETYPE_GIF:
        return array(
            'type' => IMAGETYPE_GIF,
            'resource' => imagecreatefromgif($file)
        ); // array
      case IMAGETYPE_PNG:
        return array(
            'type' => IMAGETYPE_PNG,
            'resource' => imagecreatefrompng($file)
        ); // array
    } // switch
  } // if

  return null;
} // open_image
/**
 * Resize input image
 *
 * @param string $input_file
 * @param string $dest_file
 * @param integer $max_width
 * @param integer $max_height
 * @return boolean
 */
function scale_image($input_file, $dest_file, $max_width, $max_height, $output_type = null,$quality = 80) {
  if(!extension_loaded('gd')) {
    return false;
  } // if

  $open_image = open_image($input_file);

  if(is_array($open_image)) {
    $image_type = $open_image['type'];
    $image = $open_image['resource'];

    $width  = imagesx($image);
    $height = imagesy($image);

    $scale  = min($max_width / $width, $max_height / $height);

    if($scale <1) {
      $new_width  = floor($scale * $width);
      $new_height = floor($scale * $height);

      $tmp_img = imagecreatetruecolor($new_width, $new_height);

      $white_color = imagecolorallocate($tmp_img, 255, 255, 255);
      imagefill($tmp_img, 0, 0, $white_color);
      imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      imagedestroy($image);
      $image = $tmp_img;
    } // if

    if($output_type === null) {
      $output_type = $image_type;
    } // if

    switch($output_type) {
      case IMAGETYPE_JPEG:
        return imagejpeg($image, $dest_file, $quality);
      case IMAGETYPE_GIF:
        return imagegif($image, $dest_file);
      case IMAGETYPE_PNG:
        return imagepng($image, $dest_file);
    } // switch
  } // ifs
  return false;
} // scale_image
?>